using B.S.NewMedical.Api.Write.Application.Command.ContentManagement.Registration;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace B.S.NewMedical.Api.Write.Controllers.ContentManagement.Registration
{
    /// <summary>
    /// 挂号管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RegistrationController : ControllerBase
    {
        private readonly IMediator _mediator;

        public RegistrationController(IMediator mediator)
        {
            _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
        }

        /// <summary>
        /// 添加挂号
        /// </summary>
        /// <param name="command">挂号信息</param>
        /// <returns>挂号ID</returns>
        [HttpPost]
        public async Task<IActionResult> AddRegistration([FromBody] AddRegistrationCommand command)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                var registrationId = await _mediator.Send(command);
                
                return Ok(new 
                { 
                    Success = true,
                    Message = "挂号成功",
                    Data = new { RegistrationId = registrationId }
                });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new 
                { 
                    Success = false,
                    Message = ex.Message 
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new 
                { 
                    Success = false,
                    Message = "挂号失败，请稍后重试",
                    Error = ex.Message 
                });
            }
        }
    }
} 